﻿using EagleProject.LinQDataContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EagleProject.Controllers
{
    public class HumanResourceController : MyController
    {
        //
        // GET: /HumanResource/
        public ActionResult Index()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/HumanResource");
            if (!IsHumanResource()) return RedirectToAccessDenied();

            return View();
        }

        //
        // GET: /HumanResource/ListApplication
        public ActionResult ListApplication(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/HumanResource");
            if (!IsHumanResource()) return RedirectToAccessDenied();

            int stateId = GetIntParam("stateId");
            var data = GetApplicationData(currentPageIndex, NumberOfRecordPerPage, stateId);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxApplicationList", data);
            else
            {
                ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
                return View(data);
            }
        }

        private MvcPaging.IPagedList<JobApplication> GetApplicationData(int currentPageIndex, int numberPerPage,
            int stateId)
        {
            var listData = DatabaseController.GetAllJobApplications();

            if (stateId != -1)
            {
                listData = from data in listData
                           where data.StateID == stateId
                           select data;
            }

            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }

        //
        // GET: /HumanResource/ViewApplication
        public ActionResult ViewApplication(int? id)
        {
            if (!id.HasValue) return RedirectToAction("Index");
            if (!IsAuthenticated()) return RedirectToLogin(@"~/HumanResource/ViewApplication/" + id);
            if (!IsHumanResource()) return RedirectToAccessDenied();

            var data = DatabaseController.GetApplicationByID(id.Value);

            return View(data);
        }

        //
        // GET: /HumanResource/ListConsultant
        public ActionResult ListConsultant(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/HumanResource/ListConsultant");
            if (!IsHumanResource()) return RedirectToAccessDenied();

            string sName = GetStringParam("sName");
            int stateId = GetIntParam("stateId");
            var data = GetConsultantData(currentPageIndex, NumberOfRecordPerPage, sName, stateId);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxPartnerList", data);
            else
            {
                ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
                return View(data);
            }
        }

        private MvcPaging.IPagedList<Consultant> GetConsultantData(int currentPageIndex, int numberPerPage,
            string sName, int stateId)
        {
            var listData = DatabaseController.GetConsultants();

            if (!string.IsNullOrWhiteSpace(sName))
            {
                sName = sName.ToLower();
                listData = from data in listData
                           where (data.FirstName + " " + data.MiddleName + " " + data.Lastname).ToLower().Contains(sName)
                           select data;
            }

            if (stateId != -1)
            {
                sName = sName.ToLower();
                listData = from data in listData
                           where data.LocalGovArea.StateID == stateId
                           select data;
            }

            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }

        //
        // GET: /HumanResource/ListAgent
        public ActionResult ListAgent(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/HumanResource/ListAgent");
            if (!IsHumanResource()) return RedirectToAccessDenied();

            string sName = GetStringParam("sName");
            int stateId = GetIntParam("stateId");
            var data = GetAgentData(currentPageIndex, NumberOfRecordPerPage, sName, stateId);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxAgentList", data);
            else
            {
                ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
                return View(data);
            }
        }

        private MvcPaging.IPagedList<Agent> GetAgentData(int currentPageIndex, int numberPerPage,
            string sName, int stateId)
        {
            var listData = DatabaseController.GetAgents();

            if (!string.IsNullOrWhiteSpace(sName))
            {
                sName = sName.ToLower();
                listData = from data in listData
                           where (data.FirstName + " " + data.Lastname).ToLower().Contains(sName)
                           select data;
            }

            if (stateId != -1)
            {
                listData = from data in listData
                           where data.LocalGovArea.StateID == stateId
                           select data;
            }

            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }
    }
}
